<?php

class Country_Model_DbTable_Countryfields extends Engine_Db_Table
{
  protected $_rowClass = 'Country_Model_Countryfield';
  
    public function getListFieldPage($fieldId){
        if (empty($fieldId)) {
            return null;
        }
        
        
        $countryTable  = Engine_Api::_()->getDbtable('countries','country');
        $countryName = $countryTable->info('name');
        $fieldName = $this->info('name');
        
        $select = $this->select()
                       ->setIntegrityCheck(false)  
                       ->from($fieldName,array("detail",'country_id')) 
                       ->where('field_id = ?',$fieldId)
                       ->join($countryName,"$countryName.country_id = $fieldName.country_id",array("$countryName.title as countryTitle"));
        
        $fields = $this->fetchAll($select);
        return $fields;
        
    }
    
    public function getFieldByCountry($country_id){
        $fieldTable  = Engine_Api::_()->getDbtable('fields','country');
        $fieldName = $fieldTable->info('name');
        $fieldCName = $this->info('name');
        
        $select = $this->select()
                       ->setIntegrityCheck(false)  
                       ->from($fieldCName,array("detail",'country_id')) 
                       ->where('country_id = ?',$country_id)
                       ->join($fieldName,"$fieldName.field_id = $fieldCName.field_id",array("$fieldName.title",'parent_id','field_id'));
        
        $fields = $this->fetchAll($select);
        return $fields;
    }
}
?>